package cn.iocoder.yudao.adminserver.modules.fi.dal.mysql.coreTask;

import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.adminserver.modules.fi.controller.coreTask.vo.CoreTaskExportReqVO;
import cn.iocoder.yudao.adminserver.modules.fi.controller.coreTask.vo.CoreTaskPageReqVO;
import cn.iocoder.yudao.adminserver.modules.fi.dal.dataobject.coreTask.CoreTaskDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 项目反馈 Mapper
 *
 * @author hickteam
 */
@Mapper
public interface CoreTaskMapper extends BaseMapperX<CoreTaskDO> {

    default PageResult<CoreTaskDO> selectPage(CoreTaskPageReqVO reqVO) {
        QueryWrapperX<CoreTaskDO> qw = new QueryWrapperX<>();
        StringBuilder sb = new StringBuilder( " 1=1 ");
        if(ObjectUtil.isNotNull(reqVO.getAreaManagerUid()) || ObjectUtil.isNotNull(reqVO.getProjectLeaderUid()) || ObjectUtil.isNotNull(reqVO.getDirectorUid())){
            sb.append(" and project_id in (select t.id from fi_core_project t where t.deleted=0");
            if(ObjectUtil.isNotNull(reqVO.getAreaManagerUid())){
                sb.append(" and t.area_manager_uid = "+reqVO.getAreaManagerUid());
            }
            if(ObjectUtil.isNotNull(reqVO.getProjectLeaderUid())){
                sb.append(" and t.project_leader_uid = "+reqVO.getProjectLeaderUid());
            }
            if(ObjectUtil.isNotNull(reqVO.getDirectorUid())){
                sb.append(" and t.director_uid = "+reqVO.getDirectorUid());
            }
            sb.append(")");
        }
        qw.apply(sb.toString());
        return selectPage(reqVO, qw.eq("deleted", 0)
                .eqIfPresent("maintain_id", reqVO.getMaintainId())
                .eqIfPresent("do_task_uid", reqVO.getDoTaskUid())
                .inIfPresent("project_id", reqVO.getProjectId())
                .eqIfPresent("is_finish", reqVO.getIsFinish())
                .betweenIfPresent("create_time", reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
                .likeIfPresent("do_task_desc", reqVO.getDoTaskDesc())
                .betweenIfPresent("finish_date", reqVO.getBeginFinishDate(), reqVO.getEndFinishDate())
                .eqIfPresent("task_type", reqVO.getTaskType())
                .betweenIfPresent("end_time", reqVO.getBeginEndTime(), reqVO.getEndEndTime())
                .betweenIfPresent("do_time", reqVO.getBeginDoTime(), reqVO.getEndDoTime())

                .orderByDesc("id")        );
    }

    default List<CoreTaskDO> selectList(CoreTaskExportReqVO reqVO) {
        return selectList(new QueryWrapperX<CoreTaskDO>()
                .likeIfPresent("name", reqVO.getName())
                .eqIfPresent("maintain_id", reqVO.getMaintainId())
                .eqIfPresent("do_task_uid", reqVO.getDoTaskUid())
                .inIfPresent("project_id", reqVO.getProjectId())
                .eqIfPresent("is_finish", reqVO.getIsFinish())
                .betweenIfPresent("create_time", reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
                .betweenIfPresent("finish_date", reqVO.getBeginFinishDate(), reqVO.getEndFinishDate())
                .betweenIfPresent("end_time", reqVO.getBeginEndTime(), reqVO.getEndEndTime())
                .betweenIfPresent("do_time", reqVO.getBeginDoTime(), reqVO.getEndDoTime())
                .orderByDesc("id")        );
    }

}
